package com.ufida.ids.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.hereway.core.exception.ServiceException;
import com.hereway.core.servlet.BaseServlet;
import com.hereway.core.util.ParamUtils;
import com.ufida.ids.model.OrgModel;
import com.ufida.ids.service.IOrgService;
import com.ufida.ids.util.Constants;
import com.ufida.ids.util.ExtUtil;
import com.ufida.ids.util.StringUtil;

public class OrgServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	
	private static Logger logger = Logger.getLogger(OrgServlet.class);

	@Autowired
	private IOrgService orgService;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public OrgServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
			IOException {
		//获取机构
		String action = ParamUtils.getParameter(request, "action");
		System.out.println("action:"+action);
		if ("getOrgListByPID".equals(action)){
			getOrgListByPID(request, response);
		}else if ("getOrgListByCon".equals(action)){
			getOrgListByCon(request, response);
		}else if("add".equals(action)){
			add(request, response);
		}else if("edit".equals(action)){
			edit(request, response);
		}else if("delete".equals(action)){
			delete(request, response);
		}
	}
	
	private void getOrgListByPID(HttpServletRequest request, HttpServletResponse response) {
		Long parentId = ParamUtils.getLongParameter(request, "node", 0);
		System.out.println("parentId:"+parentId);
		try {
			List<OrgModel> l = orgService.findByParentId(parentId);
			String retStr = orgService.getOrgListStr(l);
			writeResponse(response, retStr);
		} catch (ServiceException e) {
			e.printStackTrace();
		}
	}
	
	private void getOrgListByCon(HttpServletRequest request, HttpServletResponse response) {
		List resultL = new ArrayList();
		//分页参数
		int start = Integer.parseInt(request.getParameter("start"));
		int limit = Integer.parseInt(request.getParameter("limit"));	
		int pageNum = Integer.parseInt(request.getParameter("page"));	
		
		String condition = request.getParameter("condition");
		JSONObject o = JSONObject.fromObject(condition);
		String currentOrgId =  o.optString("currentOrgId");
		String orgName =  o.optString("orgName");
		
		System.out.println("orgName:"+orgName);
		System.out.println("start:"+start);
		System.out.println("limit:"+limit);
		System.out.println("currentOrgId:"+currentOrgId);
		System.out.println("pageNum:"+pageNum);
		
		try {
			String retStr = orgService.getOrgListByConPagging(currentOrgId, orgName, pageNum);
			writeResponse(response, retStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void add(HttpServletRequest request, HttpServletResponse response) {
		System.out.println("[add org...]");
		JSONObject jo = JSONObject.fromObject(request.getParameter("para"));
		JSONObject formValue = jo.optJSONObject("formContent");
		
		String pid = jo.optString("pid");
		
		String orgCode = formValue.optString("orgCode");
		String orgName = formValue.optString("orgName");
		String description = formValue.optString("description");
		String shortname = formValue.optString("shortname");
		String orgType = formValue.optString("orgType");
		String tel = formValue.optString("tel");
		String addr = formValue.optString("addr");
		
		String id = "";
		String retStr = "";
		System.out.println("pid:"+pid);
		try {
			OrgModel orgModel = new OrgModel();
			{
				orgModel.setParentOrgId(Long.parseLong(pid));
				orgModel.setOrgCode(orgCode);
				orgModel.setOrgName(orgName);
				orgModel.setDescription(description);
				orgModel.setCreator(Constants.TEMP_CREATOR);
				orgModel.setCreateTs(new Date());
				orgModel.setModifiedTs(new Date());
				orgModel.setLastModifier(Constants.TEMP_CREATOR);
				orgModel.setShortname(shortname);
				orgModel.setOrgType(Short.parseShort(orgType));
				orgModel.setAddr(addr);
				orgModel.setTel(tel);
			}
			orgService.save(orgModel);
			id = orgModel.getOrgId() + "";
			retStr = ExtUtil.getJosonStr(id, true);
		} catch (Exception e) {
			 retStr = ExtUtil.getJosonStr(false);
		}
		writeResponse(response, retStr);
		
	}
	
	private void edit(HttpServletRequest request, HttpServletResponse response) {
		System.out.println("[edit org...]");
		JSONObject jo = JSONObject.fromObject(request.getParameter("para"));
		JSONObject formValue = jo.optJSONObject("formContent");
		
		String id = jo.optString("id");
		
		String orgCode = formValue.optString("orgCode");
		String orgName = formValue.optString("orgName");
		String description = formValue.optString("description");
		String shortname = formValue.optString("shortname");
		String orgType = formValue.optString("orgType");
		String tel = formValue.optString("tel");
		String addr = formValue.optString("addr");
		
		String retStr = "";
		try {
			OrgModel orgModel = orgService.get(Long.valueOf(id));
			{
				orgModel.setOrgCode(orgCode);
				orgModel.setOrgName(orgName);
				orgModel.setDescription(description);
				orgModel.setCreator(Constants.TEMP_CREATOR);
				orgModel.setCreateTs(new Date());
				orgModel.setModifiedTs(new Date());
				orgModel.setLastModifier(Constants.TEMP_CREATOR);
				orgModel.setShortname(shortname);
				orgModel.setOrgType(Short.parseShort(orgType));
				orgModel.setAddr(addr);
				orgModel.setTel(tel);
			}
			orgService.update(orgModel);
			retStr = ExtUtil.getJosonStr(id, true);
		} catch (Exception e) {
			retStr = ExtUtil.getJosonStr(false);
		}
		writeResponse(response, retStr);
	}
	
	private void delete(HttpServletRequest request, HttpServletResponse response) {
		System.out.println("[delete org...]");
		JSONObject jo = JSONObject.fromObject(request.getParameter("para"));
		JSONArray idArr = (JSONArray)jo.opt("idArr");
		String idStr =StringUtil.buildIdsStr(idArr);
		
		String retStr = "";
		
		try {
			List l = orgService.find(idStr);
			orgService.delete(l);
			retStr = ExtUtil.getJosonStr(true);
		} catch (Exception e) {
			retStr = ExtUtil.getJosonStr(false);
		}
		writeResponse(response, retStr);
	}
}
